#include "common/def.h"

typedef struct __part_dev part_dev_t;
typedef struct __part part_t;

void (*video_printf)(const char *fmt, ...) = (void (*)(const char *fmt, ...))(0x46032DAC | 1);
void (*dprintf)(const char *fmt, ...) = (void (*)(const char* fmt, ...))(0x460333B8 | 1);
part_dev_t *(*mt_part_get_device)() = (part_dev_t * (*)())(0x46053740 | 1);
part_t *(*mt_part_get_partition)(const char *name) = (part_t * (*)(const char *name))(0x46004C30 | 1);

#define PAYLOAD_OFFSET 2097152  // keep in sync with LK_PAYLOAD_OFFSET in flash_lk_payload.py
#define PAYLOAD_ADDR 0x42000000 // keep in sync with lkp/linker.x
#define PAYLOAD_SIZE (16384*3)      // keep in sync with MAX_PAYLOAD_LEN in flash_lk_payload.py

/*uint32_t g_boot,g_recovery;
int (*read_org)(part_dev_t *dev, uint64_t src, void *dst, int size, unsigned int part_id);

int read(part_dev_t *dev, uint64_t src, void *dst, int size, unsigned int part_id) {
    dprintf("read_hook 0x%08X\n",read_org);
    return read_org(dev,src,dst,size,part_id);
}*/

int main()
{
    part_dev_t *dev = mt_part_get_device();
    part_t *part = mt_part_get_partition("boot");
    dev->read(dev, PAYLOAD_OFFSET, (uint8_t *)PAYLOAD_ADDR, PAYLOAD_SIZE, 1);
 //   dev->read(dev, PAYLOAD_OFFSET, (uint8_t *)(PAYLOAD_ADDR + PAYLOAD_SIZE), 512, part->part_id);
    volatile uint16_t *p = (volatile uint16_t *)PAYLOAD_ADDR;
    if (p[0] != 0xe002 || p[1] != 0xaa55 || p[2] != 0xaabb || p[3] != 0xccdd)
    {
        video_printf("payload missing 0x%x 0x%x 0x%x 0x%x\n", p[0], p[1], p[2], p[3]);
        for (;;)
            ;
    }

    void (*entry)() = (void (*)())(PAYLOAD_ADDR | 1);
    entry();
    for (;;)
        ;
}
